/**
 * @Method Description
 * @Author: hao zheng@
 * @Description:In User Settings Edit
 * @LastEditors: Please set LastEditors
 * @param {}
 * @return 
 * @createTime: 2021-7-9 11:15:13
 */
import {
	request
} from '@/common/js/request'
const bassUrl = require('@/common/js/config.js').bass;
import {
	msg,
	checkStr
} from "@/common/js/util.js"
export default {
	data() {
		return {
			isLoading: false, //刷新数据
			loaded: false, //加载完毕
			showtimes: false, //   获取验证码时间
			times: 59,
			setInterval: null

		}
	},
	methods: {
		/**
		 * @Description list init
		 */
		refresh() {
			this.$nextTick(() => {
				this.$refs.pullScroll.refresh(0);
			});
		},
		/**
		 * @Description get Whether to focus on;
		 * 是否相互关注
		 */
		getFocusOn(targetId) {
			// if (this.$store.sta)
			return new Promise((resolve, reject) => {
				const data = {
					uid: uni.getStorageSync('uid'),
					womanId: targetId
				}
				this.$request("user/focus-on-each-other", data).then(res => {
					if (res.resuilt == 0) {
						resolve(res.data.isFocus)
					}
				}).catch((err) => {
					reject(err);
				})
			})
		},
		/**
		 * @Description get Whether to focus on;
		 * 聊天扣金币
		 */
		chatBuckleGoldCOINS(targetId) {
			// if (this.$store.sta)
			return new Promise((resolve, reject) => {
				const data = {
					uid: uni.getStorageSync('uid'),
					womanId: targetId
				}
				this.$request("user/talk-by-text", data).then(res => {
					if (res.resuilt == 0) {
						resolve(res.data.isFocus)
					}
				}).catch((err) => {
					reject(err);
				})
			})
		},
		/**
		 * @Description get Whether to focus on;
		 * 女方回复男方确认扣金币
		 */
		herReplyToTheMan(targetId) {
			// if (this.$store.sta)
			return new Promise((resolve, reject) => {
				const data = {
					uid: uni.getStorageSync('uid'),
					womanId: targetId
				}
				this.$request("user/text-talk-reply", data).then(res => {
					if (res.resuilt == 0) {
						resolve(res.data.isFocus)
					}
				}).catch((err) => {
					reject(err);
				})
			})
		},
		/**
		 * @Description get code for use phone
		 */
		getCode(phone) {
			this.$util.throttle(async () => {
				if (this.times != 59) return;
				if (!phone) {
					this.$util.msg("请输入手机号!");
					return
				}
				if (!checkStr(phone)) {
					this.$util.msg("手机号格式错误!");
					return
				}
				const res = await this.$request('user/get-code', {
					phone
				}, {
					showLoading: true
				});
				if (res.result == 0) {
					this.$util.msg("发送成功!");
					this.showtimes = true;
					this.setInterval = setInterval(() => {
						this.times--;
						if (this.times <= 0) {
							clearInterval(this.setInterval);
							this.showtimes = false;
							this.times = 59;
						}
					}, 1000)
				} else {
					this.$util.msg(res.reaultNote);
				}
			})
		},
		/**
		 * @Description point something to window
		 */
		back() {
			setTimeout(() => {
				uni.navigateBack()
			}, 1000)
		},
		/**
		 * @Description setNavigationBarTitle
		 */
		setNavTitle(title) {
			if (!title) return;
			uni.setNavigationBarTitle({
				title
			})
		},
		/**
		 * @Description Navigate to  some page of this project and judge user has login
		 * @options {}  judge user has login
		 * @url {}  Navigate to  some page path
		 */
		navTo(url, options = {}) {
			
			this.$util.throttle(() => {
				console.log(url)
				if (!url) {
					return;
				}
				if (options.login && !uni.getStorageSync('uid')) {
					url = '/pages/public/author';
				}
				uni.navigateTo({
					url: url
				})
			}, 1000)
		},
		retTo(url, options = {}) {
			this.$util.throttle(() => {
				console.log(url)
				if (!url) {
					return;
				}
				uni.redirectTo({
					url: url
				})
			}, 1000)
		},
		launchTo(url, options = {}) {
			console.log(url)
			this.$util.throttle(() => {
				if (!url) {
					return;
				}
				uni.reLaunch({
					url: url
				})
			}, 1000)
		},
		/**
		 * @Description setClipboardData
		 * @content {}  content
		 */
		setClipboardData(content) {
			if (!content) return;
			uni.setClipboardData({
				data: content,
				success: function() {}
			});
		},
		callphone(phone) {
			console.log(phone)
			this.$util.throttle(() => {
				if (!phone) {
					return;
				}
				uni.makePhoneCall({
					phoneNumber: phone
				})
			}, 100)
		},
		bannerItemTap(item) {
			if (!uni.getStorageSync('uid')) {
				this.navTo('/pages/public/author');
				return
			}
			console.log(item)
			//  "type":""//跳转类型0富文本1xx页面2345678910
			getApp().globalData.bannerItem = item;
			if (item.type == 0) {
				this.navTo("/pages/public/picview");
			} else if (item.type == 1) {
				// this.navTo(`/pages/details/goodDetails?id=${item.goods.id}`);
			}
		},
		/**
		 * @Description get user info
		 */
		async getUserInfo() {
			// uni.setStorageSync('uid','1b10595ba97b4134a3ebc50d3911fb03');
			if (!uni.getStorageSync('uid')) return;
			const res = await this.$request('user/info', {uid:uni.getStorageSync('uid')});
			console.log(res)
			if (res.result == 0) {
				// if(res.data.interest){
				// 	res.data.interest=res.data.interest.split(",");
				// }
				// if(res.data.pic){
				// 	res.data.pic=res.data.pic.split("|");
				// }
				// if(res.data.statePic){
				// 	res.data.statePic=res.data.statePic.split('|');
				// }
				this.$store.commit('updateMyInfo', res.data);
				if (this.userInfo) this.userInfo = res.data;
			} else {
				this.$util.msg(res.resultNote);
			}
		},
		/**
		 * @Description get data from  back-end and judge user has login
		 * @options {}  judge user has login
		 * @url string request back-end interface  need path
		 * @data {}  request back-end interface need parameter
		 */
		$request(url, data = {}, options = {}) {
			if (options.showLoading) {
				this.isLoading = true;
			}
			return new Promise((resolve, reject) => {
				request(url, data, options).then(res => {
					if (res.statusCode == 200) {
						resolve(res.data)
					} else {
						this.$util.msg(`错误码:${res.statusCode}`);
						this.$refs.pullScroll && this.$refs.pullScroll.empty();
					}
					this.isLoading = false;
					this.loaded = true;
				}).catch((err) => {
					reject(err);
				})
			})
		},
		/**
		 * @Description upload file to back-end
		 * @url string   request back-end interface  need path
		 * @data {}  request back-end interface need parameter
		 */
		$uploadFile(url, data = {}) {
			console.log(data)
			return new Promise((e, n) => {
				const uploadTask = uni.uploadFile({
					url: encodeURI(bassUrl + '/lixin/api/' + url),
					filePath: data.filePath.path,
					name: 'file',
					method: "POST",
					header: {
						'Content-Type': 'multipart/form-data'
					},
					success: function(res) {
						200 == res.statusCode ? e(res.data) : msg(
							`错误码:${res.statusCode}`);
					},
					fail: function(err) {
						"request:fail " === err.errMsg && msg("请求数据失败！"), n(err
							.data);
					}
				});
				uploadTask.onProgressUpdate((res) => {
					data.progress = res.progress;
				});
			})
		},
		/**
		 * @Description the templete has  show  do  something
		 * @key string  the templete name  or ref
		 */
		showPopup(key) {
			this.$util.throttle(() => {
				this.$refs[key].open();
			}, 200)
		},
		/**
		 * @Description the templete has  hide  do  something
		 * @key string  the templete name  or ref
		 */
		hidePopup(key) {
			this.$refs[key].hidden();
		},
		stopPrevent(event) {
			event.stopPropagation();
		}
	}
}
